import jdk.internal.org.objectweb.asm.tree.MultiANewArrayInsnNode;

import java.util.Arrays;
import java.util.Scanner;


public class Test<实现二分查找> {
    public static void main30(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        //System.out.println(Arrays.toString(array));
        printArray(array);
    }
    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.println(array[i] + " ");
        }
        System.out.println();
    }
    public static void func() {
        int a = 10;
        int b = 20;
        int[] arr = new int[]{1,2,3,4};
    }

    public static void main1(String[] args) {
        int[] array4 = null;
        System.out.println(array4.length);
    }

    public static void main2(String[] args) {
        int[] array1 = {1,2,3,4};
        array1[0] = 99;

        int[] array2 = array1;
        array2[0] = 100;

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }

    public static void main3(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = {11,22,33,44};
        array1 = array2;
        array1[0] = 1888;
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }
    public static void func1(int[] array) {
        array = new int[]{15,16,17};
        //只是改变了形参的指向，并没有改变实参的指向
    }
    public static void func2(int[] array) {
        array[0] = 999;
        //此时，传递的是引用，我们通过这个引用修改了原来的值
    }
    public static void main4(String[] args) {
        int[] array1 = {1,2,3,4};
        func1(array1); //[1,2,3,4]
        //func2(array1); //[999,2,3,4]
        System.out.println(Arrays.toString(array1));
    }

    public static int[] fun10() {
        int[] tmpArr = {10,11,12};
        return tmpArr;
    }

    public static void main5(String[] args) {
        int[] array = fun10();
        System.out.println(Arrays.toString(array));
    }

    public static int[] grow(int[] array) {
        for(int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
        return array;
    }
    public static void main6(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8};
        int[] ret = grow(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }
    public static void grow2(int[] array) {
        for(int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
    }
    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8};
        grow(array);
        System.out.println(Arrays.toString(array));
    }
    public static int[] grow3(int[] array) {
        int[] tmpArray = new int[array.length];
        for(int i = 0; i < array.length; i++) {
            tmpArray[i] = array[i]*2;
        }
        return tmpArray;
    }
    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8};
        int[] ret = grow3(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }
    public static String myToString(int[] array) {
        if(array == null) {
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i != array.length-1) {
                ret += ", ";
            }
        }
        ret = ret + "]";
        return ret;
    }
    public static void main9(String[] args) {
        int[] array = {1,2,3,4};
        System.out.println(myToString(array));
    }

    public static void main10(String[] args) {
        int[] array = {1,2,3,4};
        System.out.println(Arrays.toString(array));
        int[] array2 = array;
        System.out.println(Arrays.toString(array2));
        //这不叫拷贝，这是两个变量指向同一个对象
    }

    public static void main11(String[] args) {
        int[] array = {1,2,3,4};

        int[] array2 = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            array2[i] = array[i];
        }

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
        System.out.println("==============");

        array2[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }

    public static void main12(String[] args) {
        int[] array = {1,2,3,4};
        //可以当做扩容来用
        int[] ret = Arrays.copyOf(array,array.length*2);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
        System.out.println("==============");

        ret[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main13(String[] args) {
        int[] array = {1,2,3,4};
        int[] copy = new int[array.length];
        //System.arraycopy(array,0,copy,0,array.length);
        System.arraycopy(array,0,copy,0,array.length-1);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
        System.out.println("==============");

        copy[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }
    public static void main14(String[] args) {
        int[] array = {1,2,3,4};
        int[] copy = new int[array.length];
        //System.arraycopy(array,0,copy,0,array.length);
        //支持部分拷贝的
        System.arraycopy(array,1,copy,1,array.length-1);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
        System.out.println("==============");

        copy[0] = 199;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(copy));
    }
    public static void main15(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int[] ret = Arrays.copyOfRange(array,1,3);//[1,3)
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main16(String[] args) {
        int [] array1 = {1,2,3,4};
        int [] array2 = {1,2,3,4};
        //System.out.println(array1 == array2);
        System.out.println(Arrays.equals(array1,array2));

        int[] array3 = new int[10];
        //Arrays.fill(array3,-1);
        Arrays.fill(array3,1,3,-1);//[1,3)

        System.out.println(Arrays.toString(array3));
    }

    public static void main17(String[] args) {
        int[] array1 = {1,2,3,4};
        int[] array2 = array1.clone();
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
        System.out.println("==============");

        array2[0] = 199;
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }
    // 求数组中元素的平均值
    public static void main18(String[] args) {
        int[] arr = {1,2,3,4,5,6};
        System.out.println(avg(arr));
    }
    public static double avg(int[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return (double)sum / (double)arr.length;
    }
    //查找数组中指定元素
    public static int find(int[] array,int val){
        for(int i = 0; i < array.length; i++){
            if(array[i] == val){
                return i;
            }
        }
        return -1;
    }
    public static void main19(String[] args) {
        int[] array1 = {1,12,3,14};
        int ret = find(array1,14);
        System.out.println(ret);
    }
    //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 ,
    // 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
    }
    public static void main20(String[] args) {
        int[] array = {1,2,3,4,5};
        transform(array);
        System.out.println(Arrays.toString(array));
    }
//    调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
//    如数组：[1,2,3,4,5,6]
//    调整后可能是：[1, 5, 3, 4, 2, 6]
    public static void switch1(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            while (left < right && array[left] % 2 != 0) {
                left++;
            }
            while (left < right && array[right] % 2 == 0) {
                right--;
            }
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
        }
    }

    public static void main21(String[] args) {
        int[] array = {1,2,3,4,5,6};
        switch1(array);
        System.out.println(Arrays.toString(array));
    }


    //给定一个有序整型数组, 实现二分查找
    public static int bs(int[] array,int find){
        int left = 0;
        int right = array.length;
        while(left <= right) {
            int mid = (left+right)/2;
            if(find < array[mid]){
                right = mid - 1;
            }else if(find > array[mid]) {
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static void main22(String[] args) {
        int[] array={1,3,4,5,7,9};
        System.out.println(bs(array,9));
    }
    //给定一个整型数组, 实现冒泡排序(升序排序)
    public static void bubble(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j] > arr[j+1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }
    public static void main23(String[] args) {
        int[] arr = {2,33,53,13,6,76};
        bubble(arr);
        System.out.println(Arrays.toString(arr));
    }
//    给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target 的
//    那 两个 整数，并返回它们的数组下标。
//    你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。
//    你可以按任意顺序返回答案。
//    示例 1：
//    输入：nums = [2,7,11,15], target = 9
//    输出：[0,1]
//    解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。
    public static int[] findTarget(int[] nums, int target) {
        int[] tmp = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    tmp[0] = i;
                    tmp[1] = j;
                    return tmp;
                }
            }
        }
        return tmp;
    }

    public static void main24 (String[] args) {
        int[] nums = {2,7,11,15};
        int target = 9;
        int[] ret = findTarget(nums,target);
        System.out.println(Arrays.toString(ret));

    }
    //给定一个非空整数数组，除了某个元素只出现一次以外，其余每个元素均出现两次。
    // 找出那个只出现了一次的元素。
    public static int fd(int[] arr){
        int i = 0;
        for(i = 0; i < arr.length; i++){
            int count = 0;
            int tmp = arr[i];
            for(int j = 0;j<arr.length;j++){
                if(arr[i] == arr[j]){
                    count++;
                }
            }
            if(count == 1){
                break;
            }
        }
        return arr[i];
    }
    public static void main25(String[] args) {
        int[] array = {2,3,3,2,5,6,7,6,7};
        int ret = fd(array);
        System.out.println(ret);
    }

//    给定一个大小为 n 的数组，找到其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
//    你可以假设数组是非空的，并且给定的数组总是存在多数元素。
//    示例 1：
//    输入：[3,2,3]
//    输出：3
//    示例 2：
//    输入：[2,2,1,1,1,2,2]
//    输出：2
    public static int fun1(int[] arr){
        int i = 0;
        for (i = 0; i < arr.length; i++) {
            int count = 0;
            int tmp = arr[i];
            for (int j = 0; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    count++;
                }
            }
            if (count > (arr.length / 2)) {
                break;
            }
        }
        return arr[i];
    }
    public static void main26(String[] args) {
        int[] array = {2,2,1,1,1,2,2};
        int ret = fun1(array);
        System.out.println(ret);
    }

    public static void main27(String[] args) {
        Scanner input = new Scanner(System.in);

            
        }
//        给你一个整数数组 arr，请你判断数组中是否存在连续三个元素都是奇数的情况：
//        如果存在，请返回 true ；否则，返回 false 。
//        示例 1：
//        输入：arr = [2,6,4,1]
//        输出：false
//        解释：不存在连续三个元素都是奇数的情况。
//        示例 2：
//        输入：arr = [1,2,34,3,4,5,7,23,12]
//        输出：true
//        解释：存在连续三个元素都是奇数的情况，即 [5,7,23] 。

    public static boolean findThreeOdd(int[] arr) {
        boolean ret = false;
        for (int i = 0; i < arr.length - 2; i++) {
            if (arr[i] % 2 == 1) {
                if (arr[i + 1] % 2 == 1) {
                    if (arr[i + 2] % 2 == 1) {
                        ret = true;
                        break;
                    }
                }
            }
        }
        return ret;
    }
    public static void main(String[] args) {
        int[] arr1 = {2,6,4,1};
        int[] arr2 = {1,2,34,3,4,5,7,23,12};
        boolean ret = findThreeOdd(arr1);
        System.out.println(ret);
        ret = findThreeOdd(arr2);
        System.out.println(ret);
    }


    }






















